﻿using System;
using System.Web;
using System.Web.Mvc;

namespace BigMouse.MvcWeb.Filters
{
    public class AuthorizeFilter : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool result = false;

            if (httpContext.Request.Path == "/Home/Index")
            {
                result = true;
                return result;
            }

            string token = (string)httpContext.Session["token"];

            if (token != null)
            {
                result = true;
            }
            else
            {
                result = true;
                //httpContext.Response.StatusCode = 403;
            }

            return result;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);

            if (filterContext.HttpContext.Response.StatusCode == 403)
            {
                filterContext.Result = new RedirectResult("/Home/Index");
            }
        }
    }
}